#include "maplistform.h"
#include "ui_maplistform.h"

MapListForm::MapListForm(Geocaching *aProg, QWidget *parent) :
    QWidget(parent), ui(new Ui::MapListForm), myProg(aProg)
{
    ui->setupUi(this);
    setFixedSize(size());
    connect(myProg, SIGNAL(MapAdded(Map)), this, SLOT(UpdateForm()));
    connect(myProg, SIGNAL(MapDeleted(ulong)), this, SLOT(UpdateForm()));
    connect(myProg, SIGNAL(MapEdited(Map)), this, SLOT(UpdateForm()));
    connect(myProg, SIGNAL(CurrentMapChanged()), this, SLOT(UpdateForm()));
}

MapListForm::~MapListForm()
{
    delete ui;
}

void MapListForm::activateWindow()
{
    UpdateForm();
}

void MapListForm::UpdateForm(void)
{
    ui->mapListWidget->clear();
    ui->propertyListWidget->clear();
    QList <Map> formMapList = myProg->GetMapList();
    foreach(Map tmpMap, formMapList)
    {
        QListWidgetItem *item = new QListWidgetItem(tmpMap.GetName(), ui->mapListWidget);
        item->setData(Qt::UserRole, static_cast<qulonglong>(tmpMap.GetID()));
        item->setText(tmpMap.GetName());
        if(tmpMap.GetID() == myProg->GetCurrentMapID()) item->setTextColor(Qt::darkRed);
        else                                            item->setTextColor(Qt::black);
    }
    QListWidgetItem *item = ui->mapListWidget->currentItem();
    if(!item)
    {
        ui->editMapButton->setEnabled(false);
        ui->deleteMapButton->setEnabled(false);
        ui->setCurrentButton->setEnabled(false);
    }
    else
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        ui->editMapButton->setEnabled(tmpID);
        ui->deleteMapButton->setEnabled(tmpID);
        ui->setCurrentButton->setEnabled(tmpID && tmpID != myProg->GetCurrentMapID());
    }
}

void MapListForm::on_createMapButton_clicked()
{
    CreateMapDialog cmDialog;
    cmDialog.setWindowFlags(cmDialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
    if(cmDialog.exec())
    {
        Map tmpMap;
        bool setCurrent = false;
        if(cmDialog.Result(&tmpMap, setCurrent))
            myProg->AddMap(&tmpMap, setCurrent);
    }
}

void MapListForm::on_editMapButton_clicked()
{
    QListWidgetItem *item = ui->mapListWidget->currentItem();
    if(item)
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        Map tmpMap;
        if(myProg->GetMap(tmpID, &tmpMap))
        {
            bool setCurrent = false;
            EditMapDialog emDialog(&tmpMap, tmpID == myProg->GetCurrentMapID());
            emDialog.setWindowFlags(emDialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
            if(emDialog.exec())
                if(emDialog.Result(setCurrent))
                    myProg->EditMap(tmpID, &tmpMap, setCurrent);
        }
    }
}

void MapListForm::on_deleteMapButton_clicked()
{
    QListWidgetItem *item = ui->mapListWidget->currentItem();
    if(item)
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        Map tmpMap;
        if(myProg->GetMap(tmpID, &tmpMap))
            myProg->DeleteMap(tmpID);
    }
}

void MapListForm::on_mapListWidget_doubleClicked(const QModelIndex& /*index*/)
{
    ui->propertyListWidget->clear();
    QListWidgetItem *item = ui->mapListWidget->currentItem();
    if(item)
    {
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        Map tmpMap;
        if(myProg->GetMap(tmpID, &tmpMap))
        {
            GeoCoordinates topLeft, topRight, bottomRight, bottomLeft;
            tmpMap.GetCalibration(topLeft, topRight, bottomRight, bottomLeft);
            QString tmpString;
            QListWidgetItem *itemProperty = new QListWidgetItem(tmpMap.GetName(), ui->propertyListWidget);
            itemProperty = new QListWidgetItem("Путь к файлу с изображением:", ui->propertyListWidget);
            itemProperty = new QListWidgetItem(tmpMap.GetFilePath(), ui->propertyListWidget);
            itemProperty = new QListWidgetItem("Координаты верхнего левого угла", ui->propertyListWidget);
            tmpString.sprintf("Широта: %lf", topLeft.GetLatitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            tmpString.sprintf("Долгота: %lf", topLeft.GetLongitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            itemProperty = new QListWidgetItem("Координаты верхнего правого угла", ui->propertyListWidget);
            tmpString.sprintf("Широта: %lf", topRight.GetLatitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            tmpString.sprintf("Долгота: %lf", topRight.GetLongitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            itemProperty = new QListWidgetItem("Координаты нижнего правого угла", ui->propertyListWidget);
            tmpString.sprintf("Широта: %lf", bottomRight.GetLatitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            tmpString.sprintf("Долгота: %lf", bottomRight.GetLongitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            itemProperty = new QListWidgetItem("Координаты нижнего левого угла", ui->propertyListWidget);
            tmpString.sprintf("Широта: %lf", bottomLeft.GetLatitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
            tmpString.sprintf("Долгота: %lf", bottomLeft.GetLongitude());
            itemProperty = new QListWidgetItem(tmpString, ui->propertyListWidget);
        }
    }
}

void MapListForm::on_mapFindLineEdit_returnPressed()
{
    if(!ui->mapFindLineEdit->text().isEmpty())
    {
        unsigned long findStartNumber = ui->mapListWidget->currentRow(), N = ui->mapListWidget->count();
        for(unsigned long i = ++findStartNumber; i < N; ++i)
        {
            QString tmpString = ui->mapListWidget->item(i)->text();
            tmpString.resize(ui->mapFindLineEdit->text().size());
            if(!tmpString.compare(ui->mapFindLineEdit->text()))
            {
                ui->mapListWidget->setCurrentRow(i);
                return;
            }
        }
        for(unsigned long i = 0; i < findStartNumber; ++i)
        {
            QString tmpString = ui->mapListWidget->item(i)->text();
            tmpString.resize(ui->mapFindLineEdit->text().size());
            if(!tmpString.compare(ui->mapFindLineEdit->text()))
            {
                ui->mapListWidget->setCurrentRow(i);
                return;
            }
        }
    }
}

void MapListForm::on_setCurrentButton_clicked()
{
    QListWidgetItem *item = ui->mapListWidget->currentItem();
    if(item)
    {
        myProg->SetCurrentMap(item->data(Qt::UserRole).toULongLong());
        ui->closeCurrentButton->setEnabled(true);
    }
}

void MapListForm::on_closeCurrentButton_clicked()
{
    myProg->SetCurrentMap(0);
    ui->closeCurrentButton->setEnabled(false);
}

void MapListForm::on_mapListWidget_currentRowChanged(int currentRow)
{
    ui->propertyListWidget->clear();
    if(currentRow == -1)
    {
        ui->editMapButton->setEnabled(false);
        ui->deleteMapButton->setEnabled(false);
        ui->setCurrentButton->setEnabled(false);
    }
    else
    {
        QListWidgetItem *item = ui->mapListWidget->currentItem();
        unsigned long tmpID = item->data(Qt::UserRole).toULongLong();
        ui->editMapButton->setEnabled(tmpID);
        ui->deleteMapButton->setEnabled(tmpID);
        ui->setCurrentButton->setEnabled(tmpID && tmpID != myProg->GetCurrentMapID());
    }
}
